package org.practice.validate;
/**
 * 先反射注解，根据注解信息构造相应的对象。
 * @author jack
 *
 */
public class JSValidationLengthImpl implements JSValidation{
	private int min;
    private int max;
    private String message;


    public JSValidationLengthImpl(int min, int max, String message) {
        this.min = min;
        this.max = max;
        this.message = message;
    }

    @Override
    public String getRule() {
        String defaultMinRule = "\t\t\tminlength:" + min ;
        String defaultMaxRule= "\t\t\tmaxlength:" + max ;
        if(min>0&&max>0){
            return defaultMinRule +",\n"
                    +defaultMaxRule;
        }
        if (min>0) {
            return defaultMinRule;
        }
        if(max>0){
            return defaultMaxRule;
        }
        throw new RuntimeException("Length Annotation 参数不正确，未定义min和max");
    }

    public String getMessage() {
        String defaultMinMessage = "\t\t\tminlength:\"该字段长度必须大于" + min + "\"";
        String defaultMaxMessage = "\t\t\tmaxlength:\"该字段长度必须小于" + max + "\"";
        if(min>0&&max>0){
            if (message!=null&&!message.equals("")) {
                return defaultMinMessage+",\n"
                        +defaultMaxMessage;
            }else{
                    return defaultMinMessage+",\n"
                            + defaultMaxMessage;
            }
        }
        if (min>0) {

            return defaultMinMessage;
        }
        if(max>0){

            return defaultMaxMessage;
        }
        throw new RuntimeException("Length Annotation 参数不正确，未定义min和max");
    }
}
